package logic

import (
	"context"
	"github.com/tal-tech/go-zero/core/logx"
	"shape-erver/internal/svc"
	"shape-erver/internal/types"
	"shape-erver/utils"
)

type BatchGetTaskStatusLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewBatchGetTaskStatusLogic(ctx context.Context, svcCtx *svc.ServiceContext) BatchGetTaskStatusLogic {
	return BatchGetTaskStatusLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *BatchGetTaskStatusLogic) BatchGetTaskStatus(req types.BatchGetTaskStatusReq) (*types.BatchGetTaskStatusResp, error) {
	// TODO: add your logic here and delete this line

	var taskStatusList []types.TaskStatus
	var taskStatusOne types.TaskStatus
	for _, taskId := range req.TaskIds {
		taskStatusInt, _ := utils.GetTaskGuidFromRedis(taskId)
		var taskStatus string
		var message string
		switch taskStatusInt {
		case utils.PROCESSING:
			taskStatus = "processing" // 正在生成
			message = "processing"
		case utils.FAILED:
			taskStatus = "failed" // 生成失败
			message = "failed"
		case utils.SUCCESS:
			taskStatus = "success" // 生成成功
			message = "success"
		case utils.CANCEL:
			taskStatus = "cancel" // 取消
			message = "cancel"
		case utils.QUEUED:
			taskStatus = "queued" // 排队中
			message = "queued"
		default:
			taskStatus = "unknown" // 未知
			message = "unknown"
		}
		taskStatusOne = types.TaskStatus{
			TaskId:  taskId,
			Status:  taskStatus,
			Message: message,
		}

		taskStatusList = append(taskStatusList, taskStatusOne)
	}

	return &types.BatchGetTaskStatusResp{Status: taskStatusList}, nil
}
